package com.leetcode.ask;

import java.util.ArrayList;
import java.util.List;

public class Number234 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode();
        listNode.val = 1;
        ListNode listNode1 = new ListNode();
        listNode1.val = 2;
        ListNode listNode2 = new ListNode();
        listNode2.val = 2;
        ListNode listNode3 = new ListNode();
        listNode3.val = 1;
        listNode.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = null;
        System.out.println(isPalindrome(listNode));


    }


    public static boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode t1 = head;
        ListNode reverse = new ListNode();
        List<Integer> list = new ArrayList<>();
        while (t1 != null) {
            list.add(t1.val);
            t1 = t1.next;
        }
        ListNode t2 = new ListNode();
        for (int i = list.size() - 1; i >= 0; i--) {
            if (i == list.size() - 1) {
                t2.val = list.get(i);
                reverse = t2;
                t2.next = new ListNode();
                t2 = t2.next;
            } else if (i != 0) {
                t2.val = list.get(i);
                t2.next = new ListNode();
                t2 = t2.next;

            } else {
                t2.val = list.get(i);
                t2 = null;
            }
        }
        boolean flag = true;
        while (head != null) {
            if (head.val != reverse.val) {
                flag = false;
            }
            head = head.next;
            reverse = reverse.next;
        }
        return flag;

    }

}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
